﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using Business;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Commons;
using Business.Entities;
using System.Text;

public partial class OperaEntry_Products : System.Web.UI.Page //System.Web.UI.Page
{
    DbHelper helper = DbHelper.getHelper();
    Label msgLabel;
    long iconFileNo;
    protected void Page_Load(object sender, EventArgs e)
    {
        msgLabel = (Label)Master.FindControl("MessageLabel");
        msgLabel.ForeColor = System.Drawing.Color.Red;
       // this.FileUpload.Attributes.Add("onchange", "javascript:document.getElementById('" + this.txtFileName.ClientID + "').value=this.value;");
    }
    protected void btnEdit_Click(object sender, EventArgs e)
    {
        try
        {
            Product prd = new Product();
            prd.Brandid = long.Parse(this.txtMakerId.Value);
            prd.Makerid = long.Parse(this.txtMakerId.Value);
            prd.Onstock = int.Parse(this.txtQuantity.Text);
            prd.Price = int.Parse(this.txtPrice.Text);
            prd.Productname = this.txtProductName.Text;
            prd.Description = this.txtDescription.Text;
            prd.Picture = this.iconFileNo;
            if (String.IsNullOrEmpty(this.txtProductId.Value))
            {
                helper.saveEntity(prd);
            }
            else
            {
                prd.Productid = long.Parse(this.txtProductId.Value);
                helper.updateEntity(prd);
            }
        }
        catch (Exception ex)
        {
            msgLabel.Text = ex.Message;
        }
        
    }
    protected void btnUpfile_Click(object sender, EventArgs e)
    {
        
        HttpFileCollection files = HttpContext.Current.Request.Files;
        
        try
        {
            StringBuilder strMsg = new StringBuilder();
            for (int iFile = 0; iFile < files.Count; iFile++)
            {
                Upfile file = new Upfile();
                
                file.Savepath = System.Web.HttpContext.Current.Request.MapPath("../Images") + @"\" + DateTime.Now.ToString("yyyyMMdd") + @"\";
                if (!System.IO.Directory.Exists(file.Savepath))
                {
                    System.IO.Directory.CreateDirectory(file.Savepath);
                }
                ///'检查文件扩展名字
                HttpPostedFile postedFile = files[iFile];
                string fileExtension;
                file.Orginfilename = System.IO.Path.GetFileName(postedFile.FileName);
                if (file.Orginfilename != "")
                {

                    fileExtension = System.IO.Path.GetExtension(file.Orginfilename);
                    file.Newname = DateTime.Now.ToString("yyyyMMddhhmmss") + fileExtension;
                    ///'可根据扩展名字的不同保存到不同的文件夹
                    ///注意：可能要修改你的文件夹的匿名写入权限。
                    strMsg.Append("图片保存至：“../Images/" + DateTime.Now.ToString("yyyyMMdd") + @"/" + file.Newname + "”");
                    postedFile.SaveAs(file.Savepath + file.Newname);
                    file.Description = this.txtProductName.Text;
                    file.Regdate = DateTime.Now.ToString("yyyy-MM-dd hh:mm:sss");
                    helper.saveEntity(file);
                    this.txtDescription.Text = this.txtDescription.Text + "<img src=\"../Images/" + DateTime.Now.ToString("yyyyMMdd") + "/" + file.Newname + "\">";
                    if (this.cmbIsIcon.Checked)
                    {
                        iconFileNo = helper.setToEntity<long>(helper.ExcuteQuery("select max(fileno) from upfiles"));
                    }
                }
            }
            msgLabel.Text = strMsg.ToString();
        }
        catch (System.Exception Ex)
        {
            msgLabel.Text = "文件上传失败！原因：" + Ex.Message;
        }
    }
    protected void btnProduct_Click(object sender, EventArgs e)
    {
        if (String.IsNullOrEmpty(this.txtProductId.Value))
        {
            return;
        }
        StringBuilder sql = new StringBuilder();
        sql.Append("SELECT p.productid, ");
        sql.Append("	p.typeid, ");
        sql.Append("	p.brandid, ");
        sql.Append("    b.brandname,");
        sql.Append("	p.productname, ");
        sql.Append("	p.description, ");
        sql.Append("	p.picture, ");
        sql.Append("	p.price, ");
        sql.Append("	p.onsaledate, ");
        sql.Append("	p.expiredate, ");
        sql.Append("	p.makerid, ");
        sql.Append("    m.makername,");
        sql.Append("	p.onstock, ");
        sql.Append("	p.saled, ");
        sql.Append("	p.regdate ");
        sql.Append(" FROM products p ");
        sql.Append(" LEFT JOIN makers m ");
        sql.Append(" ON p.makerid = m.makerid ");
        sql.Append(" LEFT JOIN brands b ");
        sql.Append(" ON p.brandid = b.brandid ");
        sql.Append(" LEFT JOIN types t ");
        sql.Append(" ON t.typeid = p.typeid ");
        sql.Append(" where p.productid = @productid ");
        try
        {
            ProductInfo prd = helper.setToEntity<ProductInfo>(helper.ExcuteQuery(sql.ToString(), this.txtProductId.Value));
            if (prd != null)
            {
                this.txtProductName.Text = prd.Productname;
                this.txtQuantity.Text = prd.Onstock.ToString();
                this.txtTypeId.Value = prd.Typeid.ToString();
                this.txtTypeName.Text = prd.Typename;
                this.txtPrice.Text = prd.Price.ToString();
                this.txtMakerName.Text = prd.Makername;
                this.txtMakerId.Value = prd.Makerid.ToString();
                this.txtDescription.Text = prd.Description;
                this.iconFileNo = prd.Picture;
            }
        }
        catch (Exception ex)
        {
            msgLabel.Text = ex.Message;
        }
    }
}
